package com.zhouyer.edu.service.impl;

import com.zhouyer.edu.annotation.MyAutoWired;
import com.zhouyer.edu.annotation.MyService;
import com.zhouyer.edu.annotation.MyTransactional;
import com.zhouyer.edu.dao.AccountDao;
import com.zhouyer.edu.pojo.Account;
import com.zhouyer.edu.service.TransferService;

/**
 * 银行转账 服务实现
 */
@MyService("transferService")
@MyTransactional
public class TransferServiceImpl implements TransferService {

    @MyAutoWired("jdbcAccountDaoImpl")
    private AccountDao accountDao;

    // 提供set方法，供外部注入该对象
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        // 1、分别查询到付款账户信息和收款账户信息
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);
        // 2、转账
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        // 3、保持转账后的信息
        accountDao.updateAccountByCardNo(to);
         int a = 5 / 0;
        accountDao.updateAccountByCardNo(from);
    }
}
